package br.com.relatorio.pdf.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import br.com.relatorio.beans.AcompanhamentoProcesso;
import br.com.relatorio.beans.Cliente;
import br.com.relatorio.beans.Processo;
import br.com.relatorio.enums.Status;
import br.com.relatorio.enums.StatusFaturamento;

public class GeraPdf {
	
	
	
	private Map<String, Object> parametros;
	private String jasperPath;
	private Connection connection;
	private File outputFile;
	
	
	public GeraPdf(String jasperPath, File outputFile, Map<String, Object> parametros) {
		this.jasperPath = jasperPath;
		this.outputFile = outputFile;
		this.parametros = parametros;
	}
	
	
	public File 
	exportPDF(){


		try {

			URL resource = this.getClass().getResource(jasperPath);
			InputStream fileInputStream;
			fileInputStream = (InputStream) resource.getContent();

			JasperPrint print = JasperFillManager.fillReport(fileInputStream, parametros, new JREmptyDataSource());

			FileOutputStream fileOutputStream = new FileOutputStream( outputFile );

			JRExporter exporter = new JRPdfExporter();
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
			exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fileOutputStream);
			exporter.exportReport();

			fileOutputStream.flush();
			fileOutputStream.close();
			connection.close();


		} catch (JRException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return outputFile;


	}
	
	
	public static void main(String[] args) throws JRException, IOException {
		
		Cliente cliente = new Cliente();
		cliente.setNome("Cliente TEste");
		
		Processo p = new Processo();
		p.setAno(14);
		p.setNumero(5);
		
		AcompanhamentoProcesso ap = new AcompanhamentoProcesso();
		ap.setStatus(Status.ABERTO);
		ap.setStatusFaturamento(StatusFaturamento.FATURADO);
		
		
		
		HashMap<String, Object> parametros = new HashMap<String, Object>();
		//parametros.put("cliente", cliente);
		parametros.put("NOME_CLIENTE", cliente.getNome());
		
		File file = new File("C:\\Users\\eduardo.pereira\\Documents\\relatorios\\S_relatorios\\relatorio.pdf");
		
		URL resource = GeraPdf.class.getResource("/br/com/relatorio/template/relatorio.jasper");
		
		InputStream fileInputStream;
		fileInputStream = (InputStream) resource.getContent();

		JasperPrint print = JasperFillManager.fillReport(fileInputStream, parametros, new JREmptyDataSource());
		
		FileOutputStream fileOutputStream = new FileOutputStream( file );
		
		JRExporter exporter = new JRPdfExporter();
		exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
		exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fileOutputStream);
		exporter.exportReport();

		fileOutputStream.flush();
		fileOutputStream.close();
		System.out.println(file.getName());
		System.out.println(file.getAbsolutePath());
		
		
		
	}
	
	

}
